import DS from 'ember-data';
import Ember from 'ember';
import moment from 'moment';
/*消息列表*/
var Message = DS.Model.extend({
  code: DS.attr('string'),
  business: DS.attr('string'),//业务类型
  type: DS.attr('number'),// 类型： 1 站内信  2 提醒 3电话事件
  detailType: DS.attr('number'),//详细类型（提醒类型/站内信类型） 1任务提醒2签到提醒3支付信息
  content: DS.attr('string'),// 内容
  createTime:DS.attr('number'),//创建时间
  updateTime:DS.attr('number'),//更新时间
  fromUser: DS.belongsTo('user'), //发送人
  toUser: DS.belongsTo('user'), //接收人
  hasRead:DS.attr('number'),//是否已读 1是  0否
  remark:DS.attr('string'),//备注
  num:DS.attr('number'),//未读消息数
  display_extend:true,

  typeNum:Ember.computed("type",function(){
    var type = this.get("type");
    if(type===1){
      return 1;
    }
    if(type===2){
      return 2;
    }
  }),
  detailTypeStr:Ember.computed("detailType",function(){
    var showStr = "";
    if(this.get("detailType")===1){
      showStr = "任务提醒";
    }
    if(this.get("detailType")===2){
      showStr = "签到提醒";
    }
    if(this.get("detailType")===3){
      showStr = "支付信息";
    }
    return showStr;
  }),
  contentShow:Ember.computed("content","detailType","business",function(){
    var business = this.get("business");
    var contentObj = "";
    try{
      contentObj = JSON.parse(this.get("content"));
    }catch(err){
      console.warn("parse err",err);
    }
    if(!contentObj||contentObj.length===0){
      return "";
    }
    var showStr = "";
    if(this.get("detailType")===1){
      showStr = "任务提醒:您的";
      showStr = showStr + contentObj.taskTitle;
      if(business==="taskApply"){
        showStr  = showStr + "任务已被申请";
      }else if (business==="taskApplySucc") {
        showStr  = showStr + "任务申请已通过";
      }
      else if (business==="taskApplySuccNotLocateSeat") {
        showStr  = showStr + "任务申请已通过(未分配坐席)";
      }
      else if (business==="taskApplyFail") {
        showStr  = showStr + "任务企业审核未通过";
      }
      else if (business==="taskApplyInvited") {
        showStr  = showStr + "已有企业邀请您做该任务";
      }
      else if (business==="taskApplyRefuseInvitation") {
        showStr  = showStr + "任务，所邀请的客服，已拒绝邀请";
      }
      else if (business==="taskIsPassed") {
        showStr  = showStr + "任务，审核通过未开始";
      }
      else if (business==="taskNoPass") {
        showStr  = showStr + "任务，审核不通过";
      }
      else if (business==="taskSubmitted") {
        showStr  = showStr + "任务，已提交审核";
      }
    }
    if(this.get("detailType")===2){
      showStr = "签到提醒:您的";
      showStr = showStr + contentObj.taskTitle;
      if(business==="sign"){
        showStr = showStr + "任务已开始，请及时签到";
      }
    }
    if(this.get("detailType")===3){
      showStr = "用户审核提醒:";
      if(business==="userExamineSuce"){
        showStr = showStr + "您认证审核已成功";
      }
      else if (business==="userExamineFail") {
        showStr  = showStr + "您认证审核失败，请填写正确信息";
      }
    }
    if(this.get("detailType")===4){
      showStr = "支付记录提醒:";
      if(business==="bizTypeWithdrawCash"){
        showStr = showStr + "提现已成功";
      }
      else if (business==="bizTypeWithdrawCashFailure") {
        showStr  = showStr + "提现失败";
      }
      else if (business==="bizTypeRecharge") {
        showStr  = showStr + "您的充值已成功";
      }
      else if (business==="rechargeSucc") {
        showStr  = showStr + "您的充值已成功";
      }
      else if (business==="rechargeFail") {
        showStr  = showStr + "您充值失败了";
      }
    }
    if(this.get("detailType")===5){
      showStr = "微信绑定提醒:";
      if(business==="bindWechatSucc"){
        showStr = showStr + "您的微信绑定已成功";
      }
      else if (business==="bindWechatFail") {
        showStr  = showStr + "微信绑定失败";
      }
    }
    return showStr;
  }),

 //计算时间的方法
  timeFun:function(time, format){
      var t = new Date(time);
      var tf = function(i){return (i < 10 ? '0' : '') + i};
      return format.replace(/yyyy|MM|dd|HH|mm|ss/g, function(a){
          switch(a){
              case 'yyyy':
                  return tf(t.getFullYear());
                  break;
              case 'MM':
                  return tf(t.getMonth() + 1);
                  break;
              case 'mm':
                  return tf(t.getMinutes());
                  break;
              case 'dd':
                  return tf(t.getDate());
                  break;
              case 'HH':
                  return tf(t.getHours());
                  break;
              case 'ss':
                  return tf(t.getSeconds());
                  break;
          }
      })
  },
  /*显示消息的时间*/
  signDatetime: Ember.computed('createTime',function(){
    var foundTime = this.get("createTime")*1000;
    var format = this.get("timeFun");
    var date = new Date();
    var Year=date.getFullYear();
    var Month=date.getMonth();
    var Day=date.getDate();
    var dateDiffer= new Date(Year,Month,Day);
    var dateDifferTime = dateDiffer.getTime();
    var yearDate= new Date(Year+"/01");
    var yearDateTime = yearDate.getTime();
    if(foundTime < dateDifferTime){
      return format(foundTime, 'MM-dd');
    }
    else if(foundTime < yearDateTime){
      return format(foundTime, 'yyyy MM-dd');
    }else {
      return format(foundTime, 'HH:mm');
    }
  }),
  //显示在消息 view-all  view-news
  definiteTime: Ember.computed('createTime',function(){
    var foundTime = this.get("createTime")*1000;
    var format = this.get("timeFun");
    var date = new Date();
    var Year=date.getFullYear();
    var Month=date.getMonth();
    var Day=date.getDate();
    var dateDiffer= new Date(Year,Month,Day);
    var dateDifferTime = dateDiffer.getTime();
    var yearDate= new Date(Year+"/01");
    var yearDateTime = yearDate.getTime();
    if(foundTime < dateDifferTime){
      return format(foundTime, 'MM月dd日 HH:mm');
    }
    else if(foundTime < yearDateTime){
      return format(foundTime, 'yyyy MM月dd日 HH:mm');
    }else {
      return format(foundTime, 'HH:mm');
    }
  }),
});
export default Message;
// {"callId":"109","targetNumber":"18810916398",
// "customerId":"12",
// "myNumber":"\r\n15075648575",
// "taskId":"2",
// "customerName":"徐笑天"}
